#!/usr/bin/env python
# -*- coding: utf-8 -*-
import client_update
import time

import md5
import os
import dict4ini
import client_utils

ROOT_PATH = os.getcwd()
CONFIG_FILE = os.path.join(ROOT_PATH, 'dist\update.ini')

def sumfile(f):
    '''Returns an md5 hash for an object with read() method.'''
    m = md5.new()
    fobj = open(f, 'r')
    while True:
        d = fobj.read(8096)
        if not d:
            break
        m.update(d)
    fobj.close()
    return m.hexdigest()

def make_update_file_list():
    _l = []
    for root, dirs, files in os.walk(top=os.path.join(ROOT_PATH, 'dist'), topdown=False):
        for name in files:
            _l.append(os.path.join(root, name))
    return _l
    
def main():
    
    # remove old update.ini file
    try:
        os.remove(CONFIG_FILE)
    except OSError:
        pass
    
    # open update.ini
    update_config = dict4ini.DictIni(CONFIG_FILE, format="%s:%s")
    
    # make update.ini file content
    for x in make_update_file_list():
        h = sumfile(x)
        print 'File:%s ||| Hash: %s' % (x.replace(os.path.join(ROOT_PATH, 'dist'), ''), h)
        setattr(update_config.main, h, x.replace(os.path.join(ROOT_PATH, 'dist'), ''))
    # save update.ini file 
    update_config.save()

if __name__ == '__main__':
    main()